<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page contentType="text/html" import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'list_products.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
	This is my JSP page. <br>
    <%!
    	//定义数据库驱动程序
    	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
    	//数据库连接地址
    	public static final String DBURL = "jdbc:mysql://localhost:3306/shopmarket";
    	public static final String DBUSER = "root";
    	public static final String DBPASS = "123456";
    %>
    <%
    	Connection conn = null;
    	PreparedStatement pstmt = null;
    	ResultSet rs = null;
    %>
    <%
    	try{
    		Class.forName(DBDRIVER);
    		conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
    		String sql = "select id,name,price,agio from products";
    		pstmt = conn.prepareStatement(sql);
    		rs = pstmt.executeQuery();
    %>
    <center>
    	<table>
    		<tr>
    			<td>编号</td>
    			<td>名称</td>
    			<td>价格</td>
    			<td>折扣</td>
    		</tr>
    		<%
    		while(rs.next()){
    			int id = rs.getInt(1);
    			String name = rs.getString(2);
    			Double price = rs.getDouble(3);
    			Double agio = rs.getDouble(4);
    		%>
    			<tr>
    				<td><%=id%></td>
    				<td><%=name%></td>
    				<td><%=price%></td>
    				<td><%=agio%></td>
    			</tr>
    		<%
    		}
    		%>
    	</table>
    </center>
    <%
    }catch(Exception e){
    	System.out.println(e);
    }finally{
    	rs.close();
    	pstmt.close();
    	conn.close();
    }
    %>
  </body>
</html>
